package com.ryan.msm.sevice.impl;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.ryan.msm.sevice.MsmService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class MsmServiceImpl implements MsmService {
    @Override
    public Boolean sendMsm(Map<String, String> map, String phoneNumber) {
        if(StringUtils.isEmpty(phoneNumber)) return false;

        //请记住，这里的用户accessId一定要添加短信服务的权限管理
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G5BDNwbbGXbJqoGrzTm", "S86drvpfILexGN5o9NQWLFvMxPOV3K");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", "Itrip旅行网");
        request.putQueryParameter("TemplateCode", "SMS_198365513");
        System.out.println(new Gson().toJson(map));
        request.putQueryParameter("TemplateParam", new Gson().toJson(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());//获取响应体，有助于调试
            return response.getHttpResponse().isSuccess();
        }catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }
}
